package com.ruoyi.crm.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.ruoyi.common.config.DDConstant;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.dd.MessageUtil;
import com.ruoyi.crm.domain.CrmOrder;
import com.ruoyi.crm.domain.CrmProject;
import com.ruoyi.crm.domain.CrmProjectWork;
import com.ruoyi.crm.mapper.CrmOrderMapper;
import com.ruoyi.crm.mapper.CrmProjectMapper;
import com.ruoyi.crm.mapper.CrmProjectWorkMapper;
import com.ruoyi.crm.service.ICrmProjectWorkService;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 项目维保工作日志Service业务层处理
 *
 * @author yanwenliang
 * @date 2024-08-19
 */
@Service
public class CrmProjectWorkServiceImpl implements ICrmProjectWorkService
{
    @Autowired
    private CrmProjectWorkMapper crmProjectWorkMapper;

    @Autowired
    private CrmOrderMapper crmOrderMapper;

    @Autowired
    private CrmProjectMapper crmProjectMapper;

    /**
     * 查询项目维保工作日志
     *
     * @param workId 项目维保工作日志主键
     * @return 项目维保工作日志
     */
    @Override
    public CrmProjectWork selectCrmProjectWorkByWorkId(String workId)
    {
        return crmProjectWorkMapper.selectCrmProjectWorkByWorkId(workId);
    }

    /**
     * 查询项目维保工作日志列表
     *
     * @param crmProjectWork 项目维保工作日志
     * @return 项目维保工作日志
     */
    @Override
    public List<CrmProjectWork> selectCrmProjectWorkList(CrmProjectWork crmProjectWork)
    {
        return crmProjectWorkMapper.selectCrmProjectWorkList(crmProjectWork);
    }

    /**
     * 新增项目维保工作日志
     *
     * @param crmProjectWork 项目维保工作日志
     * @return 结果
     */
    @Override
    public int insertCrmProjectWork(CrmProjectWork crmProjectWork) throws ApiException {
        crmProjectWork.setCreateTime(DateUtils.getNowDate());
        crmProjectWork.setCreateBy(SecurityUtils.getUsername());
        crmProjectWork.setUserId(SecurityUtils.getUserId());
        crmProjectWork.setWorkId(IdWorker.getIdStr());
        int i = crmProjectWorkMapper.insertCrmProjectWork(crmProjectWork);

//        if (i > 0){
//            CrmProject crmProject = crmProjectMapper.selectCrmProjectByProjectId(crmProjectWork.getProjectId());
//            CrmOrder crmOrder = crmOrderMapper.selectById(crmProject.getOrderId());
//            OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
//            msg.setMsgtype("markdown");
//            msg.setMarkdown(new OapiMessageCorpconversationAsyncsendV2Request.Markdown());
//            msg.getMarkdown().setTitle("维保服务记录");
//            StringBuilder text = new StringBuilder();
//            text.append("### " + SecurityUtils.getUsername() + "对[ " + crmOrder.getOrderName() + " ]提交了一条维保记录");
//            text.append("  \n  ");
//            text.append("[立即查看](" + DDConstant.DD_FRONT_URL + DDConstant.FRONT_URL_HOST + "crm/project?active=myChargeProject&projectId=" + crmProjectWork.getProjectId() + ")");
//            msg.getMarkdown().setText(text.toString());
//            MessageUtil.send(crmProject.getUserId(), msg);
//        }
        return i;
    }

    /**
     * 修改项目维保工作日志
     *
     * @param crmProjectWork 项目维保工作日志
     * @return 结果
     */
    @Override
    public int updateCrmProjectWork(CrmProjectWork crmProjectWork)
    {
        crmProjectWork.setUpdateTime(DateUtils.getNowDate());
        return crmProjectWorkMapper.updateCrmProjectWork(crmProjectWork);
    }

    /**
     * 批量删除项目维保工作日志
     *
     * @param workIds 需要删除的项目维保工作日志主键
     * @return 结果
     */
    @Override
    public int deleteCrmProjectWorkByWorkIds(String[] workIds)
    {
        return crmProjectWorkMapper.deleteCrmProjectWorkByWorkIds(workIds);
    }

    /**
     * 删除项目维保工作日志信息
     *
     * @param workId 项目维保工作日志主键
     * @return 结果
     */
    @Override
    public int deleteCrmProjectWorkByWorkId(String workId)
    {
        return crmProjectWorkMapper.deleteCrmProjectWorkByWorkId(workId);
    }
}
